package com.cy.java.basic;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Point{
    private int x;
    private int y;
    public Point(int x,int y){
        this.x=x;
        this.y=y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}
public class HomeWork {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        //作业:
        //1.通过反射构建Point类型的实例对象
        Class<?> c1=Class.forName("com.cy.java.basic.Point");
        Constructor<?> constructor=c1.getConstructor(int.class,int.class);
        Object o=constructor.newInstance(20,20);
        System.out.println(o);
        //2.通过反射为point类实例的属性直接赋值
        Field f1=c1.getDeclaredField("x");
        f1.setAccessible(true);
        f1.set(o,1000);
        System.out.println(o);
        //3.通过反射调用point类的实例方法为属性赋值
       Method method1= c1.getDeclaredMethod("setX",int.class);
       method1.invoke(o,2000);
        System.out.println(o);

        //4.通过反射调用point类的get方法获取属性值.
       Method method2= c1.getDeclaredMethod("getX");
       Object num=method2.invoke(o);
        System.out.println(num);



    }
}
